package home.fragment.Adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.xfengshopping.R;
import java.util.List;
import home.fragment.Goods;
import home.fragment.GoodsDetailActivity;

public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.ViewHolder> {
    private final List<Goods> goodsList;
    private final Context context;

    public GoodsAdapter(Context context, List<Goods> goodsList) {
        this.context = context;
        this.goodsList = goodsList;
        Log.d("GoodsAdapter", "适配器初始化，商品数量：" + goodsList.size());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.d("GoodsAdapter", "创建ViewHolder，视图类型：" + viewType);
        View view = LayoutInflater.from(context).inflate(R.layout.item_goods, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Goods goods = goodsList.get(position);
        Log.d("GoodsAdapter", "绑定数据，位置：" + position + "，商品名称：" + goods.getGoodsName());

        holder.tvName.setText(goods.getGoodsName());
        holder.tvPrice.setText("￥" + goods.getSellingPrice());
        Glide.with(context)
                .load(goods.getGoodsCoverImg())
                .into(holder.ivImage);

        holder.itemView.setOnClickListener(v -> {
            int goodsId = goods.getGoodsId();
            Log.d("GoodsAdapter", "点击商品，ID：" + goodsId + "，名称：" + goods.getGoodsName());

            if (goodsId <= 0) { // 加强ID校验（避免0或负数）
                Log.e("GoodsAdapter", "无效商品ID：" + goodsId);
                return;
            }

            Intent intent = new Intent(context, GoodsDetailActivity.class);
            intent.putExtra("goodsId", goodsId);

            // 新增：检查Context是否为Activity（避免ContextWrapper问题）
            if (context instanceof android.app.Activity) {
                ((android.app.Activity) context).startActivity(intent);
            } else {
                Log.e("GoodsAdapter", "Context非Activity，无法启动Activity");
                return;
            }
        });
    }

    @Override
    public int getItemCount() {
        int count = goodsList.size();
        Log.d("GoodsAdapter", "商品总数：" + count);
        return count;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final ImageView ivImage;
        public final TextView tvName, tvPrice;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivImage = itemView.findViewById(R.id.iv_goods_cover);
            tvName = itemView.findViewById(R.id.tv_goods_name);
            tvPrice = itemView.findViewById(R.id.tv_goods_price);
        }
    }
}